# include "Camera.h"
# include "WEMath.h"
# include "WEInput.h"

namespace WuguiEngine
{
	using WuguiEngine::Math::MathEx;
	using WuguiEngine::Input::InputManager;

	namespace Graphics
	{
		SimpleCamera::SimpleCamera(float aspect, float fovy, float nearP, float farP)
			:BaseCamera(aspect, fovy, nearP, farP)
		{
			position = D3DXVECTOR3(0.0f, 0.0f, 0.0f);

			D3DXVECTOR3* pAxis = new D3DXVECTOR3(0.0f, 0.0f, 1.0f);
			D3DXQuaternionRotationAxis(pOrientation, pAxis, 0.0f);
		}

		void SimpleCamera::Update(DWORD dt)
		{
			const float dAngle = 1.0f;
			const float dMove = 10.0f;

			if (InputManager::GetInstance()->IsKeyDown((int)'A'))
			{
				D3DXQUATERNION* q = new D3DXQUATERNION();
				*pOrientation *= *D3DXQuaternionRotationAxis(q, &top, 
					+MathEx::Degree2Radian(dAngle));
				isViewDirty = true;
			}

			if (InputManager::GetInstance()->IsKeyDown((int)'D'))
			{
				D3DXQUATERNION* q = new D3DXQUATERNION();
				*pOrientation *= *D3DXQuaternionRotationAxis(q, &top,
					-MathEx::Degree2Radian(dAngle));
				isViewDirty = true;
			}

			if (InputManager::GetInstance()->IsKeyDown((int)'W'))
			{
				position += front * dMove;
				isViewDirty = true;
			}

			if (InputManager::GetInstance()->IsKeyDown((int)'S'))
			{
				position -= front * dMove;
				isViewDirty = true;
			}

			if (InputManager::GetInstance()->IsKeyDown((int)'Q'))
			{
				D3DXQUATERNION* q = new D3DXQUATERNION();
				*pOrientation *= *D3DXQuaternionRotationAxis(q, &right, 
					+MathEx::Degree2Radian(dAngle));
				isViewDirty = true;
			}

			if (InputManager::GetInstance()->IsKeyDown((int)'E'))
			{
				D3DXQUATERNION* q = new D3DXQUATERNION();
				*pOrientation *= *D3DXQuaternionRotationAxis(q, &right, 
					-MathEx::Degree2Radian(dAngle));
				isViewDirty = true;
			}

			D3DXQuaternionNormalize(pOrientation, pOrientation);
			this->BaseCamera::Update(dt);
		}
	}
}